package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.StudentDao;
import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.User;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class StudentService {
	private static final StudentDao studentDao = StudentDao.getInstance();
	private static  StudentService studentService = null;

	private StudentService() {
	}

	public static StudentService getInstance() {
		if (studentService == null){
			studentService = new StudentService();
		}
		return studentService;
	}

	/**
	 * 根据条件查找Student对象
	 *
	 * @param pagination 分页信息
	 * @param conditionList_json_str 查询条件
	 * @return 查询结果
	 * @throws SQLException
	 */
	public Collection<Student> findAll(Pagination pagination,List<Condition> conditionList_json_str) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<Student> students = null;
			students = studentDao.findAll(pagination, conditionList_json_str,connection);
			JdbcHelper.close(connection);
		return students;
	}

	/**
	 * 根据id查找Student对象
	 *
	 * @param id 要查询的Student对象的id
	 * @return 查询结果
	 * @throws SQLException
	 */
	public Student find(Integer id) throws SQLException {
		//获得connection资源
		Connection connection = JdbcHelper.getConn();
		Student student =  studentDao.find(id, connection);
		JdbcHelper.close(connection);
		return student;
	}

	/**
	 * 根据对象修改Student对象
	 *
	 * @param student 要修改的Student对象
	 * @return 修改成功返回true，否则false
	 * @throws SQLException
	 */
	public boolean update(Student student) throws SQLException {
		//获得connection资源
		Connection connection = JdbcHelper.getConn();
		connection.setAutoCommit(false);
		boolean result_update = false;
		try {
			result_update = studentDao.update(student,connection);
			//提交事务
			connection.commit();

		}catch (SQLException e){
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return result_update;
	}

	/**
	 * 根据id删除Student对象
	 *
	 * @param id 要删除的Student对象
	 * @return 删除成功返回true，否则false
	 * @throws SQLException
	 */
	public boolean delete(Integer id) throws SQLException{
		Connection connection = JdbcHelper.getConn();
		connection.setAutoCommit(false);
		boolean result_del = false;
		try {
			result_del = studentDao.delete(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e){
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return result_del;
	}

	/**
	 * 添加Student对象，该对象id的值取自它超类对象的id值
	 *
	 * @param student 要添加的Student对象
	 * @return 添加成功返回true，否则false
	 * @throws SQLException
	 */
	public boolean add(Student student) throws SQLException {
		//获得进行事务的连接，添加学生和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
			student.setId(ActorService.getInstance().add());
			//创建用户对象，密码和用户名取自student字段值no
			User user = new User(student.getNo(), student.getNo(), null, student);
			Integer id = UserService.getInstance().add(user);
			//为student的id赋值
			student.setId(id);
			added=studentDao.add(student, connection);
			//提交事务
			connection.commit();
		} catch (SQLException e) {
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return added;
	}
}
